package com.olf.interpreter.execution;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.olf.openjvs.OException;

public class ConfigReader {

	public ConfigReader() {

	}

	public Map<String, String> parseConfig() throws OException {
		return readXml(0);
	}

	public Map<String, String> parseConfig(int configId) throws OException {
		return readXml(configId);
	}

	private Map<String, String> readXml(int configId) throws OException {
		// HashMap to be returned
		Map<String, String> params = new HashMap<String, String>();

		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder
					.parse(new File(
							"C:\\Users\\Ryu\\Documents\\workspace\\interpreter_logic\\src\\environment.xml"));

			// normalize text representation
			doc.getDocumentElement().normalize();
			//System.out.println("Root element of the doc is "
			//		+ doc.getDocumentElement().getNodeName());

			NodeList listOfConfig = doc.getElementsByTagName("configs");
			//int totalConfigs = listOfConfig.getLength();
			//System.out.println("Total no of configs : " + totalConfigs);

			// for (int s = 0; s < listOfConfig.getLength(); s++) {

			Node firstConfigNode = listOfConfig.item(configId);

			if (firstConfigNode == null)
				throw new OException("Invalid config ID.");
			
			if (firstConfigNode.getNodeType() == Node.ELEMENT_NODE) {

				Element firstConfElement = (Element) firstConfigNode;

				// -------
				NodeList firstCompanyList = firstConfElement
						.getElementsByTagName("AB_COMPANY");
				Element firstCompanyElement = (Element) firstCompanyList
						.item(0);

				NodeList textCompany = firstCompanyElement.getChildNodes();
				//System.out.println("First Company : "
				//		+ ((Node) textCompany.item(0)).getNodeValue().trim());

				String company = ((Node) textCompany.item(0)).getNodeValue()
						.trim().toString();

				// Adding to map
				params.put("AB_COMPANY", company);

				/*
				 * // ------- NodeList lastNameList = firstConfElement
				 * .getElementsByTagName("AB_BIN"); Element lastNameElement =
				 * (Element) lastNameList.item(0);
				 * 
				 * NodeList textLNList = lastNameElement.getChildNodes();
				 * System.out .println("Last Name : " + ((Node)
				 * textLNList.item(0)) .getNodeValue().trim());
				 * 
				 * // ---- NodeList ageList = firstConfElement
				 * .getElementsByTagName("age"); Element ageElement = (Element)
				 * ageList.item(0);
				 * 
				 * NodeList textAgeList = ageElement.getChildNodes();
				 * System.out.println("Age : " + ((Node)
				 * textAgeList.item(0)).getNodeValue() .trim());
				 * 
				 * // ------
				 */
			}// end of if clause

			// }// end of for loop with s var

		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());

		} catch (SAXException e) {
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();
			
		} catch (IOException e) {
			System.out.println("Error while acessing Config File!");
			e.printStackTrace();

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		
		return params;

	}
}
